#pragma region License
/*
MIT License
Copyright Â© 2006 The Mono.Xna Team

All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma endregion License

#ifndef RMatrix_h
#define RMatrix_h

#include <Defines.h>
#include <CSharpPropertyMethods.h>

#include <System/Collections/Generic/IEquatable.h>
#include <System/Array.h>
#include <System/Math.h>

#include "Vector2.h"
#include "Vector3.h"
#include "RQuaternion.h"

//using System;
//#if WINRT
//using System.Runtime.Serialization;
//#endif

namespace Riccsson
{
	namespace Xna
	{
		namespace Framework
		{
			//struct Vector3;
			struct Plane;

			struct Matrix
				: public Riccsson::System::Collections::Generic::IEquatable<Matrix>
			{
				#pragma region public Fields

				public: float M11;
				public: float M12;
				public: float M13;
				public: float M14;
				public: float M21;
				public: float M22;
				public: float M23;
				public: float M24;
				public: float M31;
				public: float M32;
				public: float M33;
				public: float M34;
				public: float M41;
				public: float M42;
				public: float M43;
				public: float M44;

				#pragma endregion public Fields


				#pragma region private Members

				public: static const Matrix& Identity;

				#pragma endregion private Members

				#pragma region public Constructors
        
				PROP3(Matrix, Vector3, Backward)
				PROP3(Matrix, Vector3, Down)
				PROP3(Matrix, Vector3, Forward)
				PROP3(Matrix, Vector3, Translation)
				PROP3(Matrix, Vector3, Left)
				PROP3(Matrix, Vector3, Right)
				PROP3(Matrix, Vector3, Up)


				public: Matrix()
					: PROP3_INIT(Matrix, Backward)
					, PROP3_INIT(Matrix, Down)
					, PROP3_INIT(Matrix, Forward)
					, PROP3_INIT(Matrix, Translation)
					, PROP3_INIT(Matrix, Left)
					, PROP3_INIT(Matrix, Right)
					, PROP3_INIT(Matrix, Up)
				{
				}

				public: Matrix(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31,
							  float m32, float m33, float m34, float m41, float m42, float m43, float m44)
					: PROP3_INIT(Matrix, Backward)
					, PROP3_INIT(Matrix, Down)
					, PROP3_INIT(Matrix, Forward)
					, PROP3_INIT(Matrix, Translation)
					, PROP3_INIT(Matrix, Left)
					, PROP3_INIT(Matrix, Right)
					, PROP3_INIT(Matrix, Up)
				{
					this->M11 = m11;
					this->M12 = m12;
					this->M13 = m13;
					this->M14 = m14;
					this->M21 = m21;
					this->M22 = m22;
					this->M23 = m23;
					this->M24 = m24;
					this->M31 = m31;
					this->M32 = m32;
					this->M33 = m33;
					this->M34 = m34;
					this->M41 = m41;
					this->M42 = m42;
					this->M43 = m43;
					this->M44 = m44;
				}

				COPY_CONSTRUCTOR(Matrix)
				{
					PROP3_INIT_COPY(Matrix, Vector3, Backward);
					PROP3_INIT_COPY(Matrix, Vector3, Down);
					PROP3_INIT_COPY(Matrix, Vector3, Forward);
					PROP3_INIT_COPY(Matrix, Vector3, Translation);
					PROP3_INIT_COPY(Matrix, Vector3, Left);
					PROP3_INIT_COPY(Matrix, Vector3, Right);
					PROP3_INIT_COPY(Matrix, Vector3, Up);

					this->M11 = other.M11;
					this->M12 = other.M12;
					this->M13 = other.M13;
					this->M14 = other.M14;
					this->M21 = other.M21;
					this->M22 = other.M22;
					this->M23 = other.M23;
					this->M24 = other.M24;
					this->M31 = other.M31;
					this->M32 = other.M32;
					this->M33 = other.M33;
					this->M34 = other.M34;
					this->M41 = other.M41;
					this->M42 = other.M42;
					this->M43 = other.M43;
					this->M44 = other.M44;
				}

				#pragma endregion public: Constructors


				#pragma region public: Properties
        
				public: PROP3_GET(Vector3, Backward);
				public: PROP3_SET(Vector3, Backward);
        
				public: PROP3_GET(Vector3, Down);
				public: PROP3_SET(Vector3, Down);

				public: PROP3_GET(Vector3, Forward);
				public: PROP3_SET(Vector3, Forward);

				//public: PROP3_GET(Matrix, Matrix, Identity);

				public: PROP3_GET(Vector3, Left);
				public: PROP3_SET(Vector3, Left);

				public: PROP3_GET(Vector3, Right);
				public: PROP3_SET(Vector3, Right);

				public: PROP3_GET(Vector3, Translation);
				public: PROP3_SET(Vector3, Translation);

				public: PROP3_GET(Vector3, Up);
				public: PROP3_SET(Vector3, Up);
						
				// required for OpenGL 2.0 projection matrix stuff
				// TODO: have this work correctly for 3x3 Matrices. Needs to return
				// a float[9] for a 3x3, and a float[16] for a 4x4
				public: static GC_PTR<Riccsson::System::Array<float>> ToFloatArray(Matrix mat);
				#pragma endregion public: Properties


				#pragma region public: Methods

				public: static Matrix Add(Matrix matrix1, Matrix matrix2);
				public: static void Add(Matrix ref matrix1, Matrix ref matrix2, Matrix out result);
				//public: static Matrix CreateBillboard(Vector3& objectPosition, Vector3& cameraPosition, Vector3 cameraUpVector, Nullable<Vector3> cameraForwardVector);
				//public: static void CreateBillboard(Vector3 ref objectPosition, Vector3 ref cameraPosition, Vector3 ref cameraUpVector, Nullable<Vector3> cameraForwardVector, Matrix out result);
				//public: static Matrix CreateConstrainedBillboard(Vector3& objectPosition, Vector3& cameraPosition, Vector3 rotateAxis, Nullable<Vector3> cameraForwardVector, Nullable<Vector3> objectForwardVector);
				//public: static void CreateConstrainedBillboard(Vector3 ref objectPosition, Vector3 ref cameraPosition, Vector3 ref rotateAxis, Vector3? cameraForwardVector, Vector3? objectForwardVector, Matrix out result);
				public: static Matrix CreateFromAxisAngle(Vector3& axis, float angle);
				public: static void CreateFromAxisAngle(Vector3 ref axis, float angle, Matrix out result);
				public: static Matrix CreateFromQuaternion(Quaternion quaternion);
				public: static void CreateFromQuaternion(Quaternion ref quaternion, Matrix out result);
				public: static Matrix CreateFromYawPitchRoll(float yaw, float pitch, float roll);
				public: static void CreateFromYawPitchRoll( float yaw, float pitch, float roll, Matrix out result);
				public: static Matrix CreateLookAt(Vector3& cameraPosition, Vector3& cameraTarget, Vector3& cameraUpVector);
				public: static void CreateLookAt(Vector3 ref cameraPosition, Vector3 ref cameraTarget, Vector3 ref cameraUpVector, Matrix out result);
				public: static Matrix CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane);
				public: static void CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane, Matrix out result);
				public: static Matrix CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane);
				public: static void CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane, Matrix out result);
				public: static Matrix CreatePerspective(float width, float height, float nearPlaneDistance, float farPlaneDistance);
				public: static void CreatePerspective(float width, float height, float nearPlaneDistance, float farPlaneDistance, Matrix out result);
				public: static Matrix CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance);
				public: static void CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance, Matrix out result);
				public: static Matrix CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float nearPlaneDistance, float farPlaneDistance);
				public: static void CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float nearPlaneDistance, float farPlaneDistance, Matrix out result);
				public: static Matrix CreateRotationX(float radians);
				public: static void CreateRotationX(float radians, Matrix out result);
				public: static Matrix CreateRotationY(float radians);
				public: static void CreateRotationY(float radians, Matrix out result);
				public: static Matrix CreateRotationZ(float radians);
				public: static void CreateRotationZ(float radians, Matrix out result);
				public: static Matrix CreateScale(float scale);
				public: static void CreateScale(float scale, Matrix out result);
				public: static Matrix CreateScale(float xScale, float yScale, float zScale);
				public: static void CreateScale(float xScale, float yScale, float zScale, Matrix out result);
				public: static Matrix CreateScale(Vector3& scales);
				public: static void CreateScale(Vector3 ref scales, Matrix out result);
				public: static Matrix CreateTranslation(float xPosition, float yPosition, float zPosition);
				public: static void CreateTranslation(Vector3 ref position, Matrix out result);
				public: static Matrix CreateTranslation(Vector3& position);
				public: static void CreateTranslation(float xPosition, float yPosition, float zPosition, Matrix out result);
				public: static Matrix CreateReflection(Plane& value);
				public: static void CreateReflection(Plane ref value, Matrix out result);
				public: static Matrix CreateWorld(Vector3& position, Vector3& forward, Vector3& up);
				public: static void CreateWorld(Vector3 ref position, Vector3 ref forward, Vector3 ref up, Matrix out result);
				public: float Determinant();
				public: static Matrix Divide(Matrix matrix1, Matrix matrix2);
				public: static void Divide(Matrix ref matrix1, Matrix ref matrix2, Matrix out result);
				public: static Matrix Divide(Matrix matrix1, float divider);
				public: static void Divide(Matrix ref matrix1, float divider, Matrix out result);
				public: bool Equals(Matrix other) const;
				public: override bool Equals(const Matrix* obj) const;
				public: override int GetHashCode();
				public: static Matrix Invert(Matrix matrix);
				public: static void Invert(Matrix ref matrix, Matrix out result);
				public: static Matrix Lerp(Matrix matrix1, Matrix matrix2, float amount);
				public: static void Lerp(Matrix ref matrix1, Matrix ref matrix2, float amount, Matrix out result);
				public: static Matrix Multiply(Matrix matrix1, Matrix matrix2);
				public: static void Multiply(Matrix ref matrix1, Matrix ref matrix2, Matrix out result);
				public: static Matrix Multiply(Matrix matrix1, float factor);
				public: static void Multiply(Matrix ref matrix1, float factor, Matrix out result);
				public: static Matrix Negate(Matrix matrix);
				public: static void Negate(Matrix ref matrix, Matrix out result);
				public: static Matrix Subtract(Matrix matrix1, Matrix matrix2);
				public: static void Subtract(Matrix ref matrix1, Matrix ref matrix2, Matrix out result);
				public: override GC_PTR<Riccsson::System::string> ToString();
				public: static Matrix Transpose(Matrix matrix);
				public: static void Transpose(Matrix ref matrix, Matrix out result);

				#pragma endregion public: Methods
		
				#pragma region private: Static Methods
        
				/// <summary>
				/// Helper method for using the Laplace expansion theorem using two rows expansions to calculate major and 
				/// minor determinants of a 4x4 matrix. this->Method is used for inverting a matrix.
				/// </summary>
				private: static void findDeterminants(Matrix ref matrix, float out major, 
													 float out minor1, float out minor2, float out minor3, float out minor4, float out minor5, float out minor6,
													 float out minor7, float out minor8, float out minor9, float out minor10, float out minor11, float out minor12);
		
				#pragma endregion private: Static Methods
		
				public: bool Decompose(Vector3 out scale, Quaternion out rotation, Vector3 out translation);

				Matrix operator *= (const Matrix& other);
			};

			
			Matrix operator +(Matrix matrix1, Matrix matrix2);
			Matrix operator /(Matrix matrix1, Matrix matrix2);
			Matrix operator /(Matrix matrix, float divider);
			bool operator ==(Matrix matrix1, Matrix matrix2);
			bool operator !=(Matrix matrix1, Matrix matrix2);
			Matrix operator *(Matrix matrix1, Matrix matrix2);
			Matrix operator *(Matrix matrix, float scaleFactor);
			Matrix operator -(Matrix matrix1, Matrix matrix2);
			Matrix operator -(Matrix matrix);


		}
	}
}

#endif